package postgres

import (
	"context"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
	"manager/utils"
)

var BatteryType = &batteryType{}

type batteryType struct{}

// Create 创建
func (a *batteryType) Create(ctx context.Context, m *entity.BatteryType) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *batteryType) Find(ctx context.Context, in *model.BatteryTypeInfoRequest) (*entity.BatteryType, error) {
	e := &entity.BatteryType{}

	q := GetDB(ctx).Model(&entity.BatteryType{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.Name != nil {

		q = q.Where("name like ?", in.Name)

		count++
	}

	if in.Version != nil {

		q = q.Where("version like ?", in.Version)

		count++
	}

	if in.Voltage != nil {

		q = q.Where("voltage like ?", in.Voltage)

		count++
	}

	if in.Type != nil {

		q = q.Where("type = ?", in.Type)

		count++
	}

	if in.Supplier != nil {

		q = q.Where("supplier like ?", in.Supplier)

		count++
	}

	if in.Capacity != nil {

		q = q.Where("capacity like ?", in.Capacity)

		count++
	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.First(&e).Error
	return e, err
}

// Update 更新
func (a *batteryType) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	return GetDB(ctx).Model(&entity.BatteryType{}).Where("id = ?", id).Updates(dict).Error
}

// Delete 删除
func (a *batteryType) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.BatteryType{}, id).Error
}

// List 列表查询
func (a *batteryType) List(ctx context.Context, in *model.BatteryTypeListRequest) (int, []*entity.BatteryType, error) {
	var (
		q            = GetDB(ctx).Model(&entity.BatteryType{})
		err          error
		total        int64
		batteryTypes []*entity.BatteryType
	)

	if in.Name != nil {

		q = q.Where("name like ?", utils.SqlLike(in.Name))

	}

	if in.Version != nil {

		q = q.Where("version like ?", utils.SqlLike(in.Version))

	}

	if in.Voltage != nil {

		q = q.Where("voltage like ?", in.Voltage)

	}

	if in.Type != nil {

		q = q.Where("type = ?", in.Type)

	}

	if in.Supplier != nil {

		q = q.Where("supplier like ?", in.Supplier)

	}

	if in.Capacity != nil {

		q = q.Where("capacity like ?", in.Capacity)

	}

	if in.UpdatedAt != nil {

		q = q.Where("updated_at = ?", in.UpdatedAt)

	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&batteryTypes).Error; err != nil {
		return 0, nil, err
	}
	return int(total), batteryTypes, nil
}

// ExecTransaction db事务执行
func (a *batteryType) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
